package com.jwthhealth_pub.old.diet.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.diet.view.DietSecActivity;
import com.jwthhealth_pub.old.diet.view.fragment.adatper.DietConstutionAdatper;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/12/27.
 */
public class ConstitutionFragment extends Fragment {

    public static final int JUMP = 0;
    public static final int TOAS = 1;
    public static final int ERROR = 2;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;

    private Context context;

    @BindView(R.id.rb)
    RecyclerView rv;

    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case JUMP:
                    Bundle data = msg.getData();
                    Intent intent = new Intent(context, DietSecActivity.class);
                    intent.putExtras(data);
                    startActivity(intent);
                    progressbar();
                    adapter.setLoading(false);
                    break;
                case TOAS:
                    Toast.makeText(context, msg.getData().getString(Constant.DIETDATA), Toast.LENGTH_SHORT).show();
                    progressbar();
                    adapter.setLoading(false);
                    break;
                default:
                    break;
            }
        }
    };
    private DietConstutionAdatper adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = null;

        view = inflater.inflate(R.layout.fragment_constitution, container, false);

        ButterKnife.bind(this, view);

        initRecyclerView();
        return view;

    }

    private void initRecyclerView() {
        context = getActivity();
        rv.setLayoutManager(new LinearLayoutManager(context));
        adapter = new DietConstutionAdatper(context);
        adapter.setProgressBar(progressbar);
        adapter.setmHander(mHandler);
        rv.setAdapter(adapter);
    }

    private void progressbar(){
        if (progressbar.getVisibility() == View.VISIBLE) {
            progressbar.setVisibility(View.GONE);
        } else {
            progressbar.setVisibility(View.VISIBLE);
        }
    }
}
